// pages/personal/my/index.js
const app = getApp();
const ajax = app.myRequest()
import storage from "../../../utils/storage.js";
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({
    data: {
        menu: [
            { number: '0', title: '累计天数' },
            { number: '0', title: '连续天数 ' }
        ],
        hot: [
            {
                title: '我的订单',
                text: '购买的商品记录',
                url: '/pages/personal/order/index',
                // url:'/pages/personal/development/index',
                color: 'white',
                hide: false
            },
            {
                title: '我的会员卡',
                text: '查看会员卡',
                // url:'/pages/personal/development/index',
                url: '/pages/personal/membershipCard/index',
                color: 'orange',
                hide: false
            },
            {
                title: '我的收藏',
                text: '点击查看收藏',
                // url:'/pages/personal/development/index',
                url: '/pages/personal/Collection/index',
                color: 'white',
                hide: false
            },
            {
                title: '系统消息',
                text: '查看通知消息',
                url: '/pages/personal/system/index',
                // url:'/pages/personal/development/index',
                color: 'white',
                hide: false
            },
            {
                title: '场馆入驻',
                text: '合作共赢',
                url: '/pages/login/apply/index',
                color: 'white',
                hide: false
            },
            {
                title: '教练入驻',
                text: '期待您的加入',
                // url:'/pages/personal/CoachIn/index',
                url: '/pages/personal/development/index',
                color: 'white',
                hide: false
            },
            {
                title: ' 推广有礼',
                text: '好友一起享受优惠',
                color: 'orange',
                hide: true
            },
            {
                title: '推广奖励',
                text: '一起分享得奖励',
                url: '/pages/personal/reward/index',
                color: 'orange',
                hide: true
            },
            {
                title: '帮助中心',
                text: '在线客服',
                url: '/pages/personal/membershipCard/index',
                color: 'white',
                hide: false
            },
        ],
        userName: '',
        UrlImg: '',
        statusBar: app.globalData.statusBar,
        customBar: app.globalData.customBar,
        custom: app.globalData.custom,
        show: false,
        code: false,
        service: false,
        tokenFasle: false,
        checked: false,
        cumulative_day: 0,
        month_continuity_day: 0,
        phone: null
    },

    onShow: function () {
        this.getUsername()
        this.getRecord() // 查看训练记录 
        this.judge()
        this.getToken() //获取token值
    },

    onChange(event) {
        this.setData({
            checked: event.detail,
        });
    },

    // 获取个人信息
    getUsername() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    _this.setData({ tokenFasle: true })
                    ajax.getRequest('/User/profile').then((res) => {
                        var gymnasium = res.data.data.gymnasium
                        _this.setData({
                            userName: res.data.data.nickname,
                            UrlImg: res.data.data.avatar,
                            phone: res.data.data.mobile,
                            gymnasium
                        })
                    })
                }
            }
        })
    },

    // 跳转到登录页面
    toSignIn() {
        wx.navigateTo({ url: '/pages/login/SignIn/index' })
    },

    getInto(e) {
        var _this = this
        var gymnasium = this.data.gymnasium
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    var index = e.currentTarget.dataset.idx
                    var url = _this.data.hot[index].url
                    if (index == 6) {
                        _this.setData({ show: true })
                    } else if (index == 4) { // 场馆入驻
                        if (!gymnasium) {
                            wx.navigateTo({ url: url })
                        } else {
                            if (gymnasium.status == 1) {
                                _this.setData({ admin: !0 })
                            } else {
                                Dialog.alert({ message: '您已经入驻过场馆,请勿重复提交' })
                            }
                        }
                    } else if (index == 5) { // 教练入驻
                        Dialog.alert({ message: '请到后端扫描教练入驻二维码' })
                    } else if (index == 8) { // 在线客服
                        _this.setData({ service: true })
                    } else {
                        wx.navigateTo({ url: url })
                    }
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '请登录',
                        success: function (res) {
                            if (res.confirm) {
                                wx.navigateTo({ url: '/pages/login/SignIn/index' })
                            }
                        }
                    })
                }
            }
        })
    },

    getPhoneNumber(e) { },

    // 关闭遮阴层
    signOut() {
        this.setData({ show: false })
    },

    // 查看训练记录
    getRecord() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    ajax.getRequest('/User/train').then((res) => {
                        _this.setData({
                            cumulative_day: res.data.data.cumulative_day,
                            month_continuity_day: res.data.data.month_continuity_day
                        })
                    })
                }
            }
        })
    },

    haff() {
        ajax.getRequest('/User/profile').then((res) => { })
    },

    // 判断手机号码
    judge() {
        let mobile = storage.getStorage('mobile') || this.data.phone;
        if (mobile) { this.setData({ is_mobile: true }) }
    },

    // 客服电话
    phone: function () {
        wx.makePhoneCall({
            phoneNumber: "18500255599"
        })
    },

    // 获取手机号码
    getPhoneNumber(e) {
        let that = this
        var encryptedData = e.detail.encryptedData
        var iv = e.detail.iv
        if (e.detail.errMsg == "getPhoneNumber:ok") {
            ajax.getRequest('/User/authPhone', { encryptedData, iv }).then((res) => {
                storage.setStorage('mobile', res.data.data)
                that.setData({ is_mobile: true })
                that.getUsername()
                that.getInformation()
            })
        }
    },

    // 二维码
    gteUserTo() {
        wx.navigateTo({
            url: '/pages/login/SignIn/index',
        })
    },

    // 打开我的二维码
    getInformation() {
        var _this = this
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    var user_id = wx.getStorageSync('user_id');
                    _this.setData({ code: true })
                    var page = 'pages/personal/QRcode/index'
                    var scene = user_id
                    var width = 300
                    ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
                        var QRcode = res.data.data
                        _this.setData({ QRcode })
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '请登录',
                        success: function (res) {
                            if (res.confirm) {
                                wx.navigateTo({ url: '/pages/login/SignIn/index' })
                            } else {
                                wx.showToast({ title: '取消前去登录', mask: true, icon: 'node', duration: 2000 })
                            }
                        }
                    })
                }
            }
        })
    },

    // 关闭二维码
    closeCode() {
        this.setData({ code: false })
    },

    // 关闭客服弹框
    closeService() {
        this.setData({ service: false })
    },

    // 关闭后台地址弹框
    closeAdmin() {
        this.setData({ admin: false })
    },

    // 获取TOKEN
    getToken() {
        var token = storage.getStorage('token') || null
        this.setData({ token })
    },

    // 复制登录地址
    copy: function () {
        let that = this
        wx.setClipboardData({
            data: "https://www.yuehanme.com/fqEJaieVvH.php",
            success: function (res) {
                wx.showToast({ title: '复制成功', duration: 3000 })
                that.setData({ admin: !1 })
                wx.getClipboardData({
                    success: function (res) {
                        console.log(res.data)
                    }
                })
            }
        })
    }
})